﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Text;
using Rison.Web.Common;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Entity.XT;
using Rison.BusinessFacade.Facade.XT;
using Rison.BusinessFacade.Helper;

namespace Rison.Web.admin
{
    public partial class ChangePwd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (!CurrentLoginedUser.Logined)
                {
                    this.labMsg.Text = "对不起请先登录！";
                    this.btnChange.Enabled = false;
                }
                else
                {
                    this.labUserId.Text = CurrentLoginedUser.UserId;
                }
            }
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            if (StringHelper.IsNullOrEmpty(this.txtPwd.Text))
                this.labMsg.Text = "请输入原始密码！";
            else if (!this.txtPwd.Text.Equals(CurrentLoginedUser.Password))
                this.labMsg.Text = "对不起，原始密码输入错误！";
            else if (StringHelper.IsNullOrEmpty(this.txtPwd1.Text))
                this.labMsg.Text = "请输入新密码！";
            else if (StringHelper.IsNullOrEmpty(this.txtPwd2.Text))
                this.labMsg.Text = "请输入重复密码！";
            else if (!this.txtPwd1.Text.Equals(this.txtPwd2.Text))
                this.labMsg.Text = "新密码输入错误：两次输入不一致！";
            else if (!StringHelper.IsMergeByNumberAndChar(txtPwd1.Text, "_ "))
                this.labMsg.Text = "密码只能为数字、字母、下划线的组合";
            else if (txtPwd1.Text.Length < 6)
                this.labMsg.Text = "密码长度不能小于6位！";
            else
            {
                SysUserFacade f = null; SysUser u = null;
                try
                {
                    f = new SysUserFacade();
                    u = f.Retrieve(Convert.ToInt64(CurrentLoginedUser.UID)) as SysUser;
                    if (u != null)
                    {
                        u.Password = txtPwd1.Text;
                        f.Update(u);

                        CurrentLoginedUser.Password = txtPwd1.Text;
                        this.labMsg.Text = "修改成功！";
                    }

                }
                catch { this.labMsg.Text = "修改失败：未知错误，请重试！"; }
                finally { f = null; u = null; }
            }
        }
    }
}
